package com.lvmama.pangolin.timer.processor;




import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;









import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.busi.timer.TimerManager;
import com.lvmama.pangolin.common.JSONUtil;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.Timer;
import com.lvmama.pangolin.entity.lmm.LMMCareProduct;
import com.lvmama.pangolin.lmm.service.LMMCareProductService;
import com.lvmama.pangolin.service.TimerService;
import com.lvmama.pangolin.timer.CommonJob;
import com.lvmama.pangolin.util.HttpsUtil;
import com.lvmama.pangolin.util.HttpsUtil.HttpResponseWrapper;
import com.lvmama.pangolin.util.SpringContextUtil;


public class PushCareProduct extends CommonJob {

	@Autowired
	private LMMCareProductService careProductService;
	
	@Autowired
	private TimerService timerService;
	
	private String url = "http://221.228.99.246:8246/pangolin/syn/pushNewProd";
	
	private Logger logger = Logger.getLogger(PushCareProduct.class);
	@Override
	public void runJob(JobExecutionContext context, String parameter)
	{
		logger.info("同步最新数据开始状态开始...");
		try
		{
			String timer_id  = (String)context.getJobDetail().getJobDataMap()
					.get(TimerManager.TIMER_ID);
			if(careProductService == null)
			{
				careProductService = (LMMCareProductService) SpringContextUtil.getBean(LMMCareProductService.class);
			}
			if(timerService == null)
			{
				timerService = (TimerService) SpringContextUtil.getBean(TimerService.class);
			}
			
			//把上次同步的时间点记录到Granularity_value中，下次读取是该值有效作为同步时间起点，没有使用默认一小时内更新的数据
			Date date  = new Date();
			Calendar cal =  Calendar.getInstance();
			cal.setTime(date);
			String hour = ConfigManager.getInstance().getParamValue(parameter, "HOUR","1");
			cal.add(Calendar.HOUR_OF_DAY, -Integer.valueOf(hour));
			
			String syscode=System.getProperties().getProperty("SYSCODE");
			Timer timer = new Timer();
			timer.setTimer_id(timer_id);
			timer.setSyscode(syscode);
			List<Timer> timerL = timerService.queryTimerList(timer);
			if(timerL != null && !timerL.isEmpty())
			{
				timer = timerL.get(0);
				String value = timer.getGranularity_value();
				if(StringTools.isValid(value) && StringTools.isNumeric(value))
				{
					long timeM = Long.valueOf(value);
					cal.setTimeInMillis(timeM);
				}
				
			}
			
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("updateTime", new Timestamp(cal.getTimeInMillis()));
			List<LMMCareProduct> list = careProductService.queryProductbyTime(param);
			if(list != null && !list.isEmpty())
			{
				url = ConfigManager.getInstance().getParamValue(parameter, "URL",url);
				Date pushDate = null;
				LMMCareProduct lmmCareProduct = null;
				Date update = null;
				for(int i=(list.size()-1);i>=0;i--)
				{
					try
					{
						lmmCareProduct = list.get(i);
						update = lmmCareProduct.getUpdateTime();
						lmmCareProduct.setUpdateTime(null);
						lmmCareProduct.setInsertTime(null);
						String data = JSONUtil.getJsonString4JavaPOJO(lmmCareProduct);
						HttpResponseWrapper rs = HttpsUtil.requestPostData(url, data, "application/json", "UTF-8");
						String str = rs.getResponseString();
						if(StringTools.isValid(str))
						{
							JSONObject jo = JSONUtil.getObject(str);
							String code = jo.getString("code");
							if("0".equals(code))
							{
								pushDate = update;
							}
						}
						
						
					}
					catch(Exception e)
					{
						logger.error("同步更新产品数据失败！Care_product: " + lmmCareProduct.toString());
					}
				}
				if(pushDate != null)
				{
					param.put("timer_id", timer_id);
					param.put("timer_status", "0");
					param.put("granularity_value", pushDate.getTime());
					timerService.updateTimerByCon(param);
				}
				
				
			}
			
		}catch (Exception e)
		{
			logger.error("更新任务状态失败!",e);
		}
		logger.info("同步最新数据开始状态结束...");
	}
	

}
